package team.star.werespos.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import team.star.werespos.model.entity.OrderMaster;
import team.star.werespos.model.vo.ResultVO;
import team.star.werespos.service.OrderService;
import team.star.werespos.util.Result;

import java.util.Date;
import java.util.List;

/**
 * OrderController class<br/>
 * 订单
 * 
 * @author hdonghong
 * @since 2019/05/04
 */
@Slf4j
@RequestMapping("/orders")
@RestController
public class OrderController {
    
    @Autowired
    private OrderService orderService;

    @PostMapping("/new")
    public ResultVO<?> newOrder(@RequestBody OrderMaster orderMaster) {
        orderService.insertOne(orderMaster);
        return Result.success(orderMaster);
    }


    /**
     * 获取订单列表
     * @return
     */
    @GetMapping("/list")
    public ResultVO<List<OrderMaster>> listOrderMasters(Integer orderId,
                                                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                                                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate){
        return Result.success(
                orderService.listOrders(orderId, startDate, endDate)
        );
    }

    /**
     * 加菜
     *
     */
    @PostMapping("/{orderId}/update")
    public ResultVO<?> updateById(@PathVariable Integer orderId,
                                  @RequestBody OrderMaster orderMaster) {
        orderMaster.setOrderId(orderId);
        orderService.updateById(orderMaster);
        return Result.success();
    }

    @PostMapping("/{orderId}/pay")
    public ResultVO<?> pay(@PathVariable Integer orderId) {
        orderService.pay(orderId);
        return Result.success();
    }

    @PostMapping("/{orderId}/cancel")
    public ResultVO<?> cancel(@PathVariable Integer orderId) {
        orderService.cancel(orderId);
        return Result.success();
    }

    /**
     * 删除订单
     */
    @GetMapping("/{orderId}/delete")
    public ResultVO<?> deleteById(@PathVariable Integer orderId) {
        orderService.deleteById(
                new OrderMaster().setOrderId(orderId)
        );
        return Result.success();
    }

    /**
     * 获取订单信息
     * */
    @GetMapping({"/{orderId}/profile"})
    public ResultVO<?> getProfile(
            @PathVariable(name = "orderId") Integer orderId) {
        return Result.success(orderService.getOne(orderId));
    }

    /**
     * 获取订单信息
     * */
    @GetMapping({"/profile"})
    public ResultVO<?> getProfileByTableNum(@RequestParam("tableNum") Integer tableNum) {
        return Result.success(orderService.getByTableNum(tableNum));
    }

    @PostMapping("/pay")
    public ResultVO<?> payByTableNum(@RequestParam("tableNum") Integer tableNum) {
        orderService.payByTableNum(tableNum);
        return Result.success();
    }

    @PostMapping("/cancel")
    public ResultVO<?> cancelByTableNum(@RequestParam("tableNum") Integer tableNum) {
        orderService.cancelByTableNum(tableNum);
        return Result.success();
    }

}
